-- Freeslots
freeslot("MT_COCOTREE", "MT_COCOFLOWER", "S_COCOTREE", "S_COCOFLOWER", "SPR_CMPA")
local objectNames = {{"TREE", "CMPA"}, {"FLOWER", "PGF1"}}
for i = 1, 2
    local fields = {_G["S_COCO"..objectNames[i][1]], _G["SPR_"..objectNames[i][2]], _G["MT_COCO"..objectNames[i][1]]} -- I'm lazy
    states[fields[1]] = {fields[2], A, -1, nil, 0, 0, fields[1]}
    mobjinfo[fields[3]] = {
        doomednum = 3756+(i-1),
        spawnstate = fields[1],
        flags = MF_NOBLOCKMAP|MF_NOCLIP|MF_SCENERY,
    }
end

-- MapLoad hook to load custom options
addHook("MapLoad", do
    for m in thinkers.iterate("mobj")
        if (m and m.valid) -- silver called
            if (m.type == MT_COCOTREE) -- palmtree settings
                -- left arc with ambush is flagged
                m.frame = (m.flags2 & MF2_AMBUSH) and 1 or 0
                -- bigger if object special is flagged
                m.scale = (m.spawnpoint.options & MTF_OBJECTSPECIAL) and (mapobjectscale)*7/4 or (mapobjectscale)
                m.flags = $|MF_NOTHINK -- give them MF_NOTHINK to kill off any extra processing
            elseif (m.type == MT_COCOFLOWER) -- flower settings
                -- change sprite based on flags
                if (m.flags2 & MF2_AMBUSH) -- change sprite if ambush is flagged
                    -- if objectspecial is also flagged, change sprite to PGF3, else change sprite to PGF2
                    m.sprite = (m.spawnpoint.options & MTF_OBJECTSPECIAL) and SPR_PGF3 or SPR_PGF2
                end
                -- resize the flower to be a bit bigger than the usual Peach Garden flowers
                m.scale = (mapobjectscale)*3/2
                m.flags = $|MF_NOTHINK
            end
        end
    end
end)